package com.zoshine.zzg.task;

import com.zoshine.zzg.config.ProjectConfig;
import com.zoshine.zzg.pojo.OrderPrisoner;
import com.zoshine.zzg.pojo.PrisonerInfo;
import com.zoshine.zzg.pojo.PrisonerSettlement;
import com.zoshine.zzg.pojo.RelationSupplierPrison;
import com.zoshine.zzg.pojo.pojostatus.OrderStatus;
import com.zoshine.zzg.pojo.pojostatus.PrisonerSettlementStatus;
import com.zoshine.zzg.pojo.pojostatus.RelationSupplierPrisonStatus;
import com.zoshine.zzg.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 定时任务
 *
 * @author mazefei
 * @time 2017/11/11
 */
@Component
public class ScheduledTask{
    private OrderPrisonerService orderPrisonerService;
    private PrisonerInfoService prisonerInfoService;
    private PrisonerSettlementService prisonerSettlementService;
    private RelationSupplierPrisonService relationSupplierPrisonService;
    private SystemParamService systemParamService;
    private BankService bankService;

    @Autowired
    public void setBankService(BankService bankService) {
        this.bankService = bankService;
    }

    @Autowired
    public void setSystemParamService(SystemParamService systemParamService) {
        this.systemParamService = systemParamService;
    }
    @Autowired
    public void setOrderPrisonerService(OrderPrisonerService orderPrisonerService) {
        this.orderPrisonerService = orderPrisonerService;
    }
    @Autowired
    public void setPrisonerInfoService(PrisonerInfoService prisonerInfoService) {
        this.prisonerInfoService = prisonerInfoService;
    }
    @Autowired
    public void setPrisonerSettlementService(PrisonerSettlementService prisonerSettlementService) {
        this.prisonerSettlementService = prisonerSettlementService;
    }
    @Autowired
    public void setRelationSupplierPrisonService(RelationSupplierPrisonService relationSupplierPrisonService) {
        this.relationSupplierPrisonService = relationSupplierPrisonService;
    }

    @Autowired
    private ProjectConfig projectConfig;

    private final Logger logger = LoggerFactory.getLogger("定时器");

    /**
     * 定时器运行测试
     *
     * 每5秒执行一次
     */
    @Scheduled(fixedDelay = 5000)
    public void test() {
       // logger.info("定时器运行正常。。。");
    }

    /**
     * 犯人点单自动确认收货(生成结算单)
     * 将订单状态为已发货且超过一周未确认的订单自动确认收货
     * 每天 00:10:00 执行
     */
    @Scheduled(cron = "0 10 0 * * ?")
    public void autoFinishPrisonerOrder() {
        if (isInner()) { // 只在内网运行
            OrderPrisoner orderPrisoner=new OrderPrisoner();
            orderPrisoner.setStatus(OrderStatus.FINISHED.getValue());
             List<OrderPrisoner> orderPrisoners = orderPrisonerService.list(orderPrisoner);
            for (OrderPrisoner temp:orderPrisoners){
                Long intervalMilli = new Date().getTime() - temp.getUpdateTime().getTime();
                Integer day = (int) (intervalMilli / (24 * 60 * 60 * 1000));
                if(day>systemParamService.getDay()){
                    //超过七天自动收货
                    //生成犯人结算单,一条点单生成一条结算单
                    PrisonerSettlement prisonerSettlement=new PrisonerSettlement();
                    prisonerSettlement.setOrderPrisonerId(temp.getId());
                    //判断是否生成了结算单，如果没有则生成
                    List<PrisonerSettlement> prisonerSettlements = prisonerSettlementService.list(prisonerSettlement);
                    if (prisonerSettlements.size() == 0){
                        PrisonerInfo prisonerInfo = prisonerInfoService.get(temp.getPrisonerInfo());
                        prisonerSettlement.setPrisonerAccount(temp.getPrisonerInfo());
                        prisonerSettlement.setPrisonsAccount(prisonerInfo.getOrgId());
                        prisonerSettlement.setSettlementMoney(temp.getTotalMoney());
                        prisonerSettlement.setCreateTime(new Date());
                        prisonerSettlement.setUpdateTime(new Date());
                        prisonerSettlement.setStatus(PrisonerSettlementStatus.AUDITED.getValue());
                        prisonerSettlementService.add(prisonerSettlement);
                        //更新状态为已收货
                        temp.setStatus(OrderStatus.SETTLEMENT.getValue());
                        orderPrisonerService.edit(temp);
                    }
                }
            }
        }
    }

    /**
     * 自动取消供应商与监所的关联关系
     * 将到期的关联关系状态设置为失效
     * 每天 00:05:00 执行
     */
    @Scheduled(cron = "0 5 0 * * ?")
    public void autoCancelPrisonSupplierRelation() {
        if (isOuter()) { // 只在外网运行
            logger.info("=====自动取消供应商与监所的关联关系开始======");

            logger.info("查询");
            List<RelationSupplierPrison> list = relationSupplierPrisonService.list(new RelationSupplierPrison());
            for (RelationSupplierPrison relationSupplierPrison:list){
                //当当前时间大于过期时间时则关系表失效
                if (new Date().before(relationSupplierPrison.getOverDue())){
                    relationSupplierPrison.setStatus(RelationSupplierPrisonStatus.OVERDUE.getValue());
                    relationSupplierPrisonService.edit(relationSupplierPrison);
                }
            }
            logger.info("=====自动取消供应商与监所的关联关系结束======");
        }
    }

    /**
     *1.查询开户情况
     *2.查询扣款情况
     * 每天 00:05:00 执行
     */
    @Scheduled(cron = "0 5 0 * * ?")
    public void autoInquiringBankInformation() {
        if (isBank()) { // 只在外网运行
            logger.info("=====自动查询开户情况,扣款情况======");

            logger.info("查询");
            bankService.queryAccount();
            bankService.queryPayment();
            logger.info("=====自动查询开户情况,扣款情况======");
        }
    }

    /**
     * 判断当前运行环境是否是内网
     */
    private boolean isInner() {
        String environment = projectConfig.getEnvironment();
        return Objects.equals(environment, "inner");
    }

    /**
     * 判断当前运行环境是否是外网
     */
    private boolean isOuter() {
        String environment = projectConfig.getEnvironment();
        return Objects.equals(environment, "outer");
    }

    /**
     * 判断当前运行环境是否是专线网
     */
    private boolean isBank() {
        String environment = projectConfig.getEnvironment();
        return Objects.equals(environment, "bank");
    }
}
